package com.qeeyou.apps.android.craftsman.activity.home;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.adapter.MasterAdapter;
import com.qeeyou.apps.android.craftsman.po.MasterInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchActivity extends Activity {

    private EditText editSearch;
    private Button btnCancel;
    private TextView txtvResult;
    private ListView lstvResult;
    private MasterAdapter mAdapter;
    private List<MasterInfo> craftsmanList;
    private String keyword;                         //搜索关键字
    private String pageNo;                          //页码
    private String pageSize;                        //每页数目
    private String coordinates;                     //经纬度
    private CustomLoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        setView();

        setEvent();
    }

    private void setView() {
        editSearch = (EditText) this.findViewById(R.id.editSearch4Search);
        btnCancel = (Button) this.findViewById(R.id.btnCancel4Search);
        txtvResult = (TextView) this.findViewById(R.id.txtvResult4Search);
        lstvResult = (ListView) this.findViewById(R.id.lstvResult4Search);
    }

    private void setEvent() {
        if(craftsmanList == null) {
            craftsmanList = new ArrayList<MasterInfo>();
        }
        craftsmanList.clear();
        mAdapter = new MasterAdapter(SearchActivity.this, craftsmanList);
        lstvResult.setAdapter(mAdapter);
        lstvResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                MasterInfo masterInfo = mAdapter.getItem(i);
                if(masterInfo != null) {
                    Intent intent = new Intent(SearchActivity.this, MasterDetailActivity.class);
                    intent.putExtra("masterInfo", masterInfo);
                    SearchActivity.this.startActivity(intent);
                }
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        SearchActivity.this.finish();
                    }
                }, 500);
            }
        });

        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    loadData();
                    //隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    return true;
                }
                return false;
            }
        });

    }

    private void loadData() {
        this.pageNo = "0";
        this.pageSize = "20";
        this.coordinates= "121.353164,31.132170";
        this.keyword = editSearch.getText().toString();
        String[] params = {this.pageNo, this.pageSize, this.coordinates, this.keyword};
        new GetSearchCraftsmanList().execute(params);
    }

    class GetSearchCraftsmanList extends AsyncTask<String, Void, List<MasterInfo>> {

        public GetSearchCraftsmanList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if(mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(SearchActivity.this);
            }
            mLoadingDialog.setMessage("正在搜索师傅...");
            mLoadingDialog.show();
        }

        @Override
        protected List<MasterInfo> doInBackground(String... params) {
            String pageNo = params[0];
            String pageSize = params[1];
            String coordinates = params[2];
            String keyword = params[3];
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("pageIndex", pageNo);
            paramMap.put("pageSize", pageSize);
            paramMap.put("mycoo", coordinates);
            paramMap.put("keyword", keyword);
            List<MasterInfo> masterList = AppService.getInstance().getSearchMasterList(paramMap);
            return masterList;
        }

        @Override
        protected void onPostExecute(List<MasterInfo> masterList) {
            super.onPostExecute(masterList);
            if(mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if(masterList != null && masterList.size() > 0) {

                mAdapter.setItems(masterList);
                txtvResult.setText("共搜索到" + masterList.size() + "个结果");
            } else {
                mAdapter.setItems(masterList);
                txtvResult.setText("共搜索到" + 0 + "个结果");
            }
        }
    }
}
